﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using IFRS.Models;

namespace IFRS.DataAccess.DAL
{
    public class SPR_NOID
    {
        public DataTable SEARCH_ALL(NOID noid, Paging paging, bool isAllBranch, string sCostCenter, ref int iRecordCount)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex.AsDBValue()));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize.AsDBValue()));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting.AsDBValue()));

                lstParams.Add(new SqlParameter("@isAllBranch", isAllBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@sCostCenter", sCostCenter.AsDBValue()));

                lstParams.Add(new SqlParameter("@TransactionDate", noid.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", noid.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", noid.IsConfirmed.AsDBValue()));

                dt = utility.StoredProcDataTable("SPR_NOID_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int noid)
        {
            DataTable dt;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@NOID_ID", noid));
                dt = utility.StoredProcDataTable("SPR_NOID_SEARCHBYID", lstParams.ToArray());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(NOID noid)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@NOID_ID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;

                lstParams.Add(new SqlParameter("@TransactionDate", noid.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccount", noid.GLAccount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", noid.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidAmount", noid.UnpaidAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CurrencyCode", noid.CurrencyCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreditLevelID", noid.CreditLevelID.AsDBValue()));
                lstParams.Add(new SqlParameter("@LoanObjectiveID", noid.LoanObjectiveID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractLoanTypeID", noid.ContractLoanTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ReservedAmount", noid.ReservedAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", noid.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_GSB_PRDCD", noid.RP_GSB_PRDCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_SOURCE_FILE", noid.RP_SOURCE_FILE.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_MAPPING_CD", noid.RP_MAPPING_CD.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", noid.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", noid.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", noid.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", noid.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", noid.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", noid.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", noid.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", noid.UpdatedBranch.AsDBValue()));

                utility.ExecuteStoredProc("SPR_NOID_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(NOID noid)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@NOID_ID", noid.NOID_ID.AsDBValue()));
                lstParams.Add(new SqlParameter("@TransactionDate", noid.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccount", noid.GLAccount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", noid.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidAmount", noid.UnpaidAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CurrencyCode", noid.CurrencyCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreditLevelID", noid.CreditLevelID.AsDBValue()));
                lstParams.Add(new SqlParameter("@LoanObjectiveID", noid.LoanObjectiveID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractLoanTypeID", noid.ContractLoanTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ReservedAmount", noid.ReservedAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", noid.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_GSB_PRDCD", noid.RP_GSB_PRDCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_SOURCE_FILE", noid.RP_SOURCE_FILE.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_MAPPING_CD", noid.RP_MAPPING_CD.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", noid.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", noid.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", noid.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", noid.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_NOID_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(NOID noid)
        {
            int iNum;
            var utility = new IFRS_Utility();
            var lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@NOID_ID", noid.NOID_ID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", noid.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", noid.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", noid.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", noid.UpdatedBranch.AsDBValue()));

                iNum = (int)utility.ExecuteStoredProc("SPR_NOID_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}
